package com.quanyan.ic.vendingMachine.http.controller;

import com.quanyan.ic.common.http.APIResponse;
import com.quanyan.ic.common.http.enums.ApiResponseEnum;
import com.quanyan.ic.vendingMachine.http.service.VmUpgradeService;
import com.quanyan.ic.vendingMachine.model.UpgradeVersionReq;
import com.quanyan.ic.vendingMachine.model.UpgradeVersionResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2017/8/23.
 */
@RestController
@RequestMapping("/larkic/vendingMachine/upgrade")
public class VmVersionUpgradeController {

    @Autowired
    private VmUpgradeService vmUpgradeService;

    @RequestMapping("/version")
    public APIResponse getMaxFileTypeVersion(@RequestBody UpgradeVersionReq version, HttpServletRequest request) {
        return APIResponse.returnSuccess(vmUpgradeService.getUpgradeInfo(version, request));
    }

    /**
     * 上传数据
     * @param req
     * @param file
     * @return
     */
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public APIResponse uploadFileInfo(@RequestBody UpgradeVersionResp req, MultipartFile file)
     {
         if(StringUtils.isEmpty(req.getVmNumber())||StringUtils.isEmpty(req.getFileType())
                 ||StringUtils.isEmpty(req.getVersionCode())||StringUtils.isEmpty(req.getName())){
             return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
         }
         if(file.isEmpty()){
             return APIResponse.returnFail(ApiResponseEnum.RESOURCE_NOT_FOUND);
         }
        return APIResponse.returnSuccess(vmUpgradeService.uploadFileToType(file,req));
    }

    @RequestMapping("/download/{fileType}/{fileType_1}/{fileName:.*}")
    public APIResponse download_1( @PathVariable String fileType,@PathVariable String fileType_1,@PathVariable String fileName,HttpServletResponse request) {
        if(StringUtils.isEmpty(fileName)){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        fileType=fileType+"/"+fileType_1;
        return APIResponse.returnSuccess(vmUpgradeService.fileDownload(fileType, fileName,request));
    }

    @RequestMapping("/download/{fileType}/{fileName:.*}")
    public APIResponse download_2( @PathVariable String fileType,@PathVariable String fileName,HttpServletResponse request) {
        if(StringUtils.isEmpty(fileName)){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        return APIResponse.returnSuccess(vmUpgradeService.fileDownload(fileType, fileName,request));
    }
    /**
     * 推送下发版本
     * @param req
     * @return
     */
    @RequestMapping("/sendCmd")
    public APIResponse  sendUpgradeCmd(@RequestBody UpgradeVersionReq req){
        if(StringUtils.isEmpty(req.getVmNumber())||StringUtils.isEmpty(req.getFileType())
                ){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
       ;
        return APIResponse.returnSuccess( vmUpgradeService.sendUpgradeCmd(req));
    }


}
